import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  String helloText = 'hello'.trParams({'name': '张三', 'country': '中国'});

  AppBar _setAppBar() {
    return AppBar(
      leading: Padding(
        padding: EdgeInsets.all(12),
        child: InkWell(
          onTap: () {
            FlutterAppBadger.updateBadgeCount(2);
            print('==================');

            FlutterAppBadger.isAppBadgeSupported()
                .then((value) => print(value));
          },
          child: ClipRRect(
            borderRadius: BorderRadius.circular(30),
            child: Image(
              image: AssetImage('images/avatar.png'),
            ),
          ),
        ),
      ),
      title: Center(
        child: Container(
          height: 36.0,
          width: double.infinity,
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(5)),
          child: TextField(
            keyboardType: TextInputType.text,
            autofocus: false,
            decoration: InputDecoration(
              //输入里面输入文字内边距设置
              contentPadding: EdgeInsets.only(left: 12.0),
              hintText: '留下你的精彩评论吧',
              hintStyle: TextStyle(
                fontSize: 14,
              ),
              counterText: '',
              //不需要输入框下划线
              border: InputBorder.none,
              //左边图标设置
              prefixIcon: Icon(Icons.search, color: Colors.grey),
            ),
            onTap: () {
              Get.toNamed('/search');
            },
            showCursor: false,
            maxLength: 18,
            readOnly: true,
            inputFormatters: [LengthLimitingTextInputFormatter(18)],
          ),
        ),
      ),
      actions: [
        SizedBox(
          height: 30.0,
          width: 30.0,
          child: IconButton(
            onPressed: () {},
            padding: EdgeInsets.all(0.0),
            icon: Icon(
              Icons.explore_outlined,
              size: 24.0,
              color: Colors.grey,
            ),
          ),
        ),
        Padding(
          padding: EdgeInsets.only(right: 8.0),
          child: SizedBox(
            height: 30.0,
            width: 30.0,
            child: IconButton(
              onPressed: () {
                Get.toNamed('/stopWatch');
              },
              padding: EdgeInsets.all(0.0),
              icon: Icon(
                Icons.mail_outline,
                size: 24.0,
              ),
            ),
          ),
        )
      ],
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    print('${Get.locale}, didChaneDependencies');
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _setAppBar(),
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            Center(
              child: Container(
                child: Text('This is a Container'),
                height: 60,
                alignment: Alignment.center,
              ),
            ),
            Center(
              child: InkWell(
                onTap: () {
                  print('This is a InkWell');
                },
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(24),
                  child: Image(
                    height: 36,
                    width: 36,
                    image: AssetImage('images/avatar.png'),
                  ),
                ),
              ),
            ),
            Container(
              height: 40.0,
              child:
                  Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                Icon(Icons.explore_outlined, size: 24.0),
                Icon(Icons.explore_outlined, size: 24.0),
                Icon(Icons.explore_outlined, size: 24.0),
              ]),
            ),
            Container(
              child:
                  Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                SizedBox(
                  height: 30.0,
                  width: 30.0,
                  child: IconButton(
                    onPressed: () {},
                    padding: EdgeInsets.all(0.0),
                    icon: Icon(Icons.explore_outlined, size: 24.0),
                  ),
                ),
                SizedBox(
                  height: 30.0,
                  width: 30.0,
                  child: Container(
                    child: IconButton(
                      color: Colors.redAccent,
                      onPressed: () {},
                      padding: EdgeInsets.all(0.0),
                      icon: Icon(Icons.explore_outlined, size: 24.0),
                    ),
                  ),
                ),
              ]),
            ),
            SizedBox(
              height: 12.0,
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20.0),
              child: Column(children: [
                Center(
                  child: Text(
                    'dart'.tr,
                    style: TextStyle(fontSize: 16.0),
                  ),
                ),
                Center(
                  child: Text(
                    helloText,
                    style: TextStyle(fontSize: 16.0),
                  ),
                )
              ]),
            ),
            SizedBox(
              height: 12.0,
            ),
            ElevatedButton(
                onPressed: () {
                  if ('zh_CN' == Get.locale.toString()) {
                    Get.updateLocale(Locale('en', 'US'));
                    setState(() {
                      helloText = 'hello'
                          .trParams({"name": "R1cardo", "country": "Germany"});
                    });
                  } else {
                    Get.updateLocale(Locale('zh', 'CN'));
                    setState(() {
                      helloText =
                          'hello'.trParams({'name': '张三', 'country': '中国'});
                    });
                  }
                },
                child: Text("切换到英文/中文")),
          ],
        ),
      ),
    );
  }
}
